﻿using System;
using System.Data;
using System.Configuration;
using Dcms.Common;
using Dcms.BLL;
using Dcms.Model;
using System.Web.Security;
using WebApp.AppCode;

public partial class Admin_UserManage_EditUsers : System.Web.UI.Page
{
    public CompetenceLib CL = new CompetenceLib();
    Dcms_AdminBLL aBll = new Dcms_AdminBLL();
    Dcms_AdminModel aModel = new Dcms_AdminModel();
    protected void Page_Load(object sender, EventArgs e)
    {

        CL.IsLoginMain();

        if (!IsPostBack)
        {
            aModel = aBll.GetModelByID(CL.getSessionModle().LoginID);
            this.txt_AdminEmail.Text = aModel.Admin_Email;
            this.txt_AdminMsn.Text = aModel.Admin_Msn;
            this.txt_AdminQQ.Text = aModel.Admin_QQ;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        aModel = aBll.GetModelByID(CL.getSessionModle().LoginID);
        aModel.Admin_Id = CL.getSessionModle().LoginID;
        aModel.Admin_Email = this.txt_AdminEmail.Text;
        aModel.Admin_Msn = this.txt_AdminMsn.Text;
        aModel.Admin_QQ = this.txt_AdminQQ.Text;

        aBll.Update(aModel);

        Session.Add("Message", "用户资料修改成功！");
        Session.Add("Onclick", "UserManage/EditUsers.aspx?");
        Response.Redirect("../Message.aspx",false);
    }

    protected void btnUpdatePwd_Click(object sender, EventArgs e)
    {
        string strOldPwd = txt_OldPwd.Text.Trim();
        if (strOldPwd == "")
        {
            RegisterStartupScript(this.ToString(), "<script>alert('请输入旧密码！');</script>");
        }
        string strNewPwd = txt_NewPwd1.Text.Trim();
        if (strNewPwd == "")
        {
            RegisterStartupScript(this.ToString(), "<script>alert('请输入新密码！');</script>");
        }
        string strNewPwd1 = txt_NewPwd2.Text.Trim();
        if (strNewPwd1 == "")
        {
            RegisterStartupScript(this.ToString(), "<script>alert('请输入重复密码！');</script>");
        }
        if (strNewPwd != strNewPwd1)
        {
            RegisterStartupScript(this.ToString(), "<script>alert('两次输入的密码不相同，请重输！');</script>");
        }
        string strUserName = CL.getSessionModle().LoginName;
        //string IsUCenter = ConfigurationManager.AppSettings["IsUCenter"].ToUpper();
        //if (IsUCenter == "TRUE")
        //{
        //    string strMsg = "";
        //    string strBackKey = ConfigurationManager.AppSettings["BackKey"];

        //    if (UCenterOperate.ChangePassword(strUserName, strOldPwd, strNewPwd, strBackKey, out strMsg) == true)
        //    {
        //        aModel = aBll.GetModelByName(strUserName);
        //        if (aModel != null)
        //        {
        //            aBll.UpdatePwd(aModel.Admin_Id, FormsAuthentication.HashPasswordForStoringInConfigFile(strNewPwd, "md5").ToLower());

        //            Session.Add("Message", "密码修改完成,请重新登录！");
        //            Session.Add("Onclick", "javascript:window.open('AdminLogin.aspx','_top');");
        //            Session.Add("EDITPASSOK", true);
        //        }
        //        else
        //        {
        //            Session.Add("Message", "旧密码输入错误，请重新输入！");
        //            Session.Add("Url", "UserManage/EditUsers.aspx?");
        //        }

        //        Response.Redirect("../Message.aspx", false);
        //    }
        //    else
        //    {
        //        RegisterStartupScript(this.ToString(), "<script>alert('" + strMsg + "');</script>");
        //    }
        //}
        //else
        //{
            aModel = aBll.GetModelByName(strUserName);
            if (aModel != null)
            {
                aBll.UpdatePwd(aModel.Admin_Id, FormsAuthentication.HashPasswordForStoringInConfigFile(strNewPwd, "md5").ToLower());

                Session.Add("Message", "密码修改完成,请重新登录！");
                Session.Add("Onclick", "javascript:window.open('AdminLogin.aspx','_top');");
                Session.Add("EDITPASSOK", true);
            }
            else
            {
                Session.Add("Message", "旧密码输入错误，请重新输入！");
                Session.Add("Url", "UserManage/EditUsers.aspx?");
            }

            Response.Redirect("../Message.aspx", false);
        //}
    }
}
